#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <string.h>

// gslX680 触摸屏一般是 ABS 绝对坐标设备
int main(void)
{
    const char *dev = "/dev/input/event0";  // 触摸屏设备
    int fd = open(dev, O_RDONLY);
    if(fd < 0) {
        perror("无法打开触摸屏设备");
        return 1;
    }

    struct input_event ev;
    int x = -1, y = -1;

    printf("触摸屏检测开始，点击屏幕查看坐标...\n");

    while(1) {
        ssize_t n = read(fd, &ev, sizeof(ev));
        if(n == (ssize_t)sizeof(ev)) {
            if(ev.type == EV_ABS) {
                if(ev.code == ABS_X) x = ev.value;
                if(ev.code == ABS_Y) y = ev.value;
            }
            if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0) {
                // 松开手指，打印一次坐标
                printf("Touch release at x=%d, y=%d\n", x, y);
                fflush(stdout);
            }
            if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 1) {
                // 按下
                printf("Touch press at x=%d, y=%d\n", x, y);
                fflush(stdout);
            }
        }
    }

    close(fd);
    return 0;
}

